﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

namespace SmartProject.Entity
{
    /// <summary>
    /// This is the message received by the user.
    /// </summary>
    public class Message : EntityBase
    {
        /// <summary>
        /// Gets or sets the owner of this message.
        /// </summary>
        public virtual User Owner { get; set; }

        /// <summary>
        /// Gets or sets the recipient of this message.
        /// </summary>
        public virtual User Recipient { get; set; }

        /// <summary>
        /// Gets or sets the subject line for this message.
        /// </summary>
        public virtual string Subject { get; set; }

        /// <summary>
        /// Gets or sets the descrioption for this message.
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// Gets or sets the timestamp that this message was opened.
        /// </summary>
        public virtual DateTime? Opened { get; set; }

        /// <summary>
        /// Gets a message by a known Id value.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="messageId">Message Id.</param>
        /// <returns>Message instance.</returns>
        public static Message GetMessage(ISession session, int messageId)
        {
            return (Message)EntityBase.GetEntity(session, messageId, typeof(Message));
        }

        /// <summary>
        /// Creates a new message with the required parameters.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="owner">Owner username of this message.</param>
        /// <param name="subject">Subject for this message.</param>
        /// <param name="description">Description or actual message.</param>
        /// <returns>Message instance.</returns>
        public static Message NewMessage(ISession session, string owner, string subject, string description)
        {
            return NewMessage(session, User.GetUserByUsername(session, owner), subject, description);
        }

        /// <summary>
        /// Creates a new message.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="owner">Owner instance of this message.</param>
        /// <param name="subject">Subject for this message.</param>
        /// <param name="description">Description or actual message.</param>
        /// <returns>Message instance.</returns>
        public static Message NewMessage(ISession session, User owner, string subject, string description)
        {
            Message message = new Message();
            message.Owner = owner;
            message.Subject = subject;
            message.Description = description;
            message.Save(session);
            return message;
        }

        /// <summary>
        /// Mark this message as opened by the recipient.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        public virtual void MarkAsOpened(ISession session)
        {
            this.Opened = DateTime.Now;
            Save(session);
        }
    }
}
